

<!DOCTYPE html>
<html class="writer-html5" lang="zh-CN" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>4. 基本AIL程序 &mdash; AIL-docs  文档</title>
  

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />

  
  
  
  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="5. 基本输入输出语句" href="basicstmt.html" />
    <link rel="prev" title="3. 存储和使用变量" href="variable.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home" alt="Documentation Home"> AIL-docs
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../whatsnew/index.html">版本介绍</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">入门教程</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="setup.html">1. 搭建AIL运行环境</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic.html">2. 将AIL用于计算</a></li>
<li class="toctree-l2"><a class="reference internal" href="variable.html">3. 存储和使用变量</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">4. 基本AIL程序</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#id1">4.1. 编写AIL程序</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id2">4.2. 运行AIL程序</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id3">4.3. AIL程序结构</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id4">4.4. AIL语句</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id5">4.5. AIL语句块</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="basicstmt.html">5. 基本输入输出语句</a></li>
<li class="toctree-l2"><a class="reference internal" href="controlflow.html">6. 基本流程控制语句</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../library/index.html">标准库参考</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/index.html">语言参考</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">AIL-docs</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="index.html">AIL 教程</a> &raquo;</li>
        
      <li><span class="section-number">4. </span>基本AIL程序</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="../_sources/tutorial/program.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="ail">
<h1><span class="section-number">4. </span>基本AIL程序<a class="headerlink" href="#ail" title="永久链接至标题">¶</a></h1>
<p>在体验了AIL牌计算器后，相信你已经懂得如何使用一些基本类型和变量了。接下来，就是开始真正了解AIL，使用AIL编写程序的时候了。</p>
<div class="section" id="id1">
<h2><span class="section-number">4.1. </span>编写AIL程序<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2>
<p>AIL程序，实际上就是一个文本文件，你可以使用任何的文字编辑器来编写AIL程序，如vim, sublime text, atom这类文本编辑器，当然，Windows记事本也是允许的。</p>
<p>通常，AIL程序以 <code class="code docutils literal notranslate"><span class="pre">.ail</span></code> 作为后缀。</p>
</div>
<div class="section" id="id2">
<h2><span class="section-number">4.2. </span>运行AIL程序<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h2>
<p>在编写完AIL程序后，接下来要做的就是运行程序。</p>
<p>运行AIL程序，首先你得确保你安装了AIL解释器。</p>
<p>现在假设你编写好了一个程序，<code class="code docutils literal notranslate"><span class="pre">hello.ail</span></code> ，那么运行它，只需要在终端输入：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ ail hello.ail
Hello World!
</pre></div>
</div>
<p>不出意外，你会看到这个程序的输出（如果你为这个程序设置了输出）。</p>
<p>下面是 <code class="code docutils literal notranslate"><span class="pre">hello.ail</span></code> 的源码，可以写入到文件中，使用AIL运行：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">begin</span>

<span class="nb">print</span> <span class="s1">&#39;Hello World!&#39;</span>

<span class="n">end</span>
</pre></div>
</div>
<p><em>有关AIL程序的细节，将在稍后讲解。</em></p>
</div>
<div class="section" id="id3">
<h2><span class="section-number">4.3. </span>AIL程序结构<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
<p>AIL程序的结构非常简单。对于标准AIL程序， <strong>必须以 begin 开头，以 end 结尾</strong> 。就像上面的 <code class="code docutils literal notranslate"><span class="pre">hello.ail</span></code> 一样。</p>
<p>被 begin 和 end 包裹着的，就是AIL程序的主要逻辑部分了。这一部分主要由一个一个语句组成，在下面的章节，将会具体阐述这些相关内容。</p>
</div>
<div class="section" id="id4">
<h2><span class="section-number">4.4. </span>AIL语句<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h2>
<p>语句是AIL程序的基本元素，一条条语句组合，可以实现各种各样的操作。</p>
</div>
<div class="section" id="id5">
<h2><span class="section-number">4.5. </span>AIL语句块<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h2>
<p>语句块是AIL组织语句的一种方式，语句块由 <code class="code docutils literal notranslate"><span class="pre">{</span> <span class="pre">}</span></code> 包裹着。</p>
<p>语句块具有从属性，一般来说，没有孤独的语句块。比如用于分支判断的if语句，或者用于进行循环的while语句，都拥有自己的语句块。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="n">true</span> <span class="p">{</span>
    <span class="nb">print</span> <span class="s1">&#39;inside while&#39;</span>
<span class="p">}</span>
<span class="nb">print</span> <span class="s1">&#39;outside while&#39;</span>
</pre></div>
</div>
<p>需要注意的是，在书写语句块的时候，应该严格遵循格式规定。比如对于 while 语句，语句块的左花括号，应该与 while 同一行，否则将产生语法错误！</p>
<p>当然，除了推荐的花括号外，传统的单词型分界也是支持的，比如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span> <span class="n">then</span>
    <span class="nb">print</span> <span class="s1">&#39;yes!&#39;</span>
<span class="n">endif</span>
</pre></div>
</div>
<p>但是对于单词型分界，要求会更多。具体要看每个语句对单词的要求。</p>
</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="basicstmt.html" class="btn btn-neutral float-right" title="5. 基本输入输出语句" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="variable.html" class="btn btn-neutral float-left" title="3. 存储和使用变量" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        
        &copy; 版权所有 2020, ChenHongbo

    </p>
  </div>
    
    
    
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>